package org.wangp.framework.core.util

import com.baomidou.mybatisplus.core.metadata.IPage
import org.modelmapper.TypeToken
import org.wangp.framework.core.util.modelMapper

/**
 * @author wangpeng (๑•ᴗ•๑)
 * @date 2021/10/10 18:02
 */
infix fun <S, T> IPage<S>.mapTo(target: Class<T>): T = modelMapper.map(this, target)

infix fun <S, T> IPage<S>.mapToType(typeToken: TypeToken<T>): T = modelMapper.map(this, typeToken.type)

infix fun <S, T> Iterable<S>.mapTo(target: Class<T>): Iterable<T> =
	this.map { modelMapper.map(it, target) }

fun <S, T> Iterable<S>.mapTo(target: Class<T>, cover: (S, T) -> Unit): Iterable<T> =
	this.map {
		val s = modelMapper.map(it, target)
		cover(it, s)
		s
	}

infix fun <S, T> Iterable<S>.mapToType(typeToken: TypeToken<T>): Iterable<T> =
	this.map { modelMapper.map(it, typeToken.type) }

fun <S, T> Iterable<S>.mapToType(typeToken: TypeToken<T>, cover: (S, T) -> Unit): Iterable<T> =
	this.map {
		val t: T = modelMapper.map(it, typeToken.type)
		cover(it, t)
		t
	}
